﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Excel;
using System.Data;
using OfficeOpenXml;

public class CnToEn : MonoBehaviour {

	
    static List<DictionaryDateS> mDictionary = new List<DictionaryDateS>();
    static List<ExeclDate> AllExeclDate = new List<ExeclDate>();
    static Dictionary<string, ExeclDate> NotTraned = new Dictionary<string, ExeclDate>();
    static int DicCount = 0;

    [MenuItem( "AiFeng/Transform CN To VN" )]
    static void TransCNtoVN()
    {
#if !VN
		Debug.Log("需先加宏定义 VN ");
		return;
#endif
        ReadXLSX();
       // RePlaceScriptsText();
       // RePlaceSceneLabelText();
       // RePlaceAssetsLabelText();
       // WriteXLSX();
        AssetDatabase.Refresh();
    }

    //读取xlsx
    static void ReadXLSX()
    {
        mDictionary.Clear();
        AllExeclDate.Clear();
        NotTraned.Clear();
        DicCount = 1;
        string ExeclPaths = Application.dataPath.Replace( "Assets", "AssetsVN" ) + "/已翻译好的Execl/";
        try
        {
            string[] AllExecls = Directory.GetFiles( ExeclPaths, "*.xlsx", SearchOption.AllDirectories );
            foreach( string path in AllExecls )
            {
                ExcelRead( path );
            }
            //			Debug.Log (mDictionary.Count);
        }
        catch( System.Exception error )
        {
            Debug.Log( error.Message );
        }
    }

    static void ExcelRead( string path )
    {
        FileStream stream = File.Open( path, FileMode.Open, FileAccess.Read );
        if( stream != null )
        {
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader( stream );
            if( excelReader != null )
            {
                do
                {
                    //					Debug.Log("Sheet Name: " + excelReader.Name);
                    while( excelReader.Read() )
                    {
                        DictionaryDateS date = new DictionaryDateS();
                        for( int i = 0; i < excelReader.FieldCount; i++ )
                        {
                            string content = excelReader.IsDBNull( i ) ? "" : excelReader.GetString( i );
                            if( string.IsNullOrEmpty( content ) )
                            {
                                continue;
                            }
                            if( i == excelReader.FieldCount - 2 )
                            {
                                date.Key_CN = content;
                            }
                            else if( i == excelReader.FieldCount - 1 )
                            {
                                date.Key_VN = content;
                            }
                        }
                        if( !string.IsNullOrEmpty( date.Key_CN ) && !string.IsNullOrEmpty( date.Key_VN ) )
                        {
                            date.Index = DicCount;
                            Debug.Log( DicCount + " CN: " + date.Key_CN + " VN: " + date.Key_VN );
                            mDictionary.Add( date );
                            DicCount++;
                        }
                        if( path.Contains( "客户端需要重新翻译的中文夹越南文" ) )
                        {
                            DictionaryDateS newdate = new DictionaryDateS();
                            for( int i = 0; i < excelReader.FieldCount; i++ )
                            {
                                string content = excelReader.IsDBNull( i ) ? "" : excelReader.GetString( i );
                                if( string.IsNullOrEmpty( content ) )
                                {
                                    continue;
                                }
                                if( i == 0 )
                                {
                                    newdate.Key_CN = content;
                                }
                                else if( i == excelReader.FieldCount - 1 )
                                {
                                    newdate.Key_VN = content;
                                }
                            }
                            if( !string.IsNullOrEmpty( newdate.Key_CN ) && !string.IsNullOrEmpty( newdate.Key_VN ) )
                            {
                                newdate.Index = DicCount;
                                Debug.Log( DicCount + " CN: " + newdate.Key_CN + " VN: " + newdate.Key_VN );
                                mDictionary.Add( newdate );
                                DicCount++;
                            }
                        }
                    }
                }
                while( excelReader.NextResult() );
            }
        }
    }
}
public class DictionaryDateS
{
    public int Index = 0;
    public string Key_CN = "";
    public string Key_VN = "";
}
